import 'dart:typed_data';

import 'package:aichat_flutter_wallet/kai_print.dart';
import 'package:aichat_flutter_wallet/module/module_news/model/remark_item.dart';
import 'package:aichat_flutter_wallet/module/module_news/provider/news_provider.dart';
import 'package:aichat_flutter_wallet/module/module_news/utils/news_util.dart';
import 'package:aichat_flutter_wallet/module/module_news/widget/new_commemt_widget.dart';
import 'package:aichat_flutter_wallet/module_common/utils/colors.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/image/public_image.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/public_tip/public_tip.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lib_util/lib_util.dart';

import '../../../module_common/bridge/native_channel_bridge.dart';

class NewDetailRemarkH2 extends StatefulWidget {
  RemarkItem remarkItem;
  Function onchange;
  String articleId;
  Function? doCommentSuccess;
  NewDetailRemarkH2(
      {Key? key, required this.remarkItem, required this.onchange, required this.articleId,this.doCommentSuccess})
      : super(key: key);

  @override
  _NewDetailRemarkH2State createState() => _NewDetailRemarkH2State();
}

class _NewDetailRemarkH2State extends State<NewDetailRemarkH2> {
  Uint8List? videoImage;
  NewsProvider? newsProvider;
  @override
  void initState() {
    super.initState();
    newsProvider = context.read<NewsProvider>();
    getVideoImage();
  }

  getVideoImage() async {
    if (widget.remarkItem.videos.isNotEmpty) {
      videoImage = await NewsUtil.getVideoThumb(widget.remarkItem.videos[0]);
    }
    if (mounted) setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          ClipRRect(
            borderRadius: BorderRadius.circular(28),
            child: PublicImage(
              image: widget.remarkItem.pic,
              width: 28.w,
              height: 28.w,
            ),
          ),
          SizedBox(
            width: 8.w,
          ),
          Expanded(
              child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                children: [
                  Text(
                    NewsUtil.getName(widget.remarkItem.user_name),
                    style:
                        TextStyle(fontSize: 12.sp, color: AppColors.c_868391),
                  ),
                  SizedBox(
                    width: 4.w,
                  ),
                  PublicImage(
                    image: 'assets/images/common/news_@.png',
                    width: 9.w,
                    height: 9.w,
                  ),
                  SizedBox(
                    width: 4.w,
                  ),
                  Text(
                    // '@${widget.remarkItem.target_user_name}',
                    "@${NewsUtil.getName(widget.remarkItem.target_user_name)}",
                    style:
                        TextStyle(fontSize: 12.sp, color: AppColors.c_847F9A),
                  )
                ],
              ),
              SizedBox(
                height: 5.w,
              ),
              Text(widget.remarkItem.content),
              // 视频或者图片
              if (widget.remarkItem.images.isNotEmpty)
                ClipRRect(
                  borderRadius: BorderRadius.circular(10.w),
                  child: InkWell(
                    splashColor: Colors.transparent,
                    onTap: () {
                      NativeChannelBridge().flPreviewMedia(type: 'image',index: 0.toString(),datas: widget.remarkItem.images);
                    },
                    child: PublicImage(
                      image: widget.remarkItem.images[0],
                      width: double.infinity,
                      height: 164.h,
                    ),
                  ),
                ),
              if (videoImage != null)
                newsProvider!.getVideoWidget(
                    videoImg: videoImage!,
                    callback: () {
                      NativeChannelBridge().flPreviewMedia(type: 'video',index: 0.toString(),datas: widget.remarkItem.videos);
                    }),
              if (newsProvider != null)
              SizedBox(
                height: 8.w,
              ),
              if (newsProvider != null)
                newsProvider!.getRemarkFooter(widget.remarkItem,
                    doLike: (value) {
                  doLike(widget.remarkItem);
                }, doComment: (res) {
                  NewsUtil.showCommentDialog(
                      ctx: context,
                      toName: widget.remarkItem!.user_name??'',
                      success: (CommemtTypeCallBackData value) {
                    kaiPrint('value2===${value}');
                    doComment(res,
                        content: value.content,
                        videos: value.videos,
                        images: value.images);
                  });
                }),
              SizedBox(
                height: 17.w,
              )
            ],
          ))
        ],
      ),
    );
  }

  doComment(RemarkItem value,
      {String? content, List<String>? videos, List<String>? images}) {
    newsProvider!.doComment(
        images: images ?? [],
        videos: videos ?? [],
        target_type: widget.remarkItem.target_type,
        level: 2,
        target_id: widget.remarkItem.id,
        article_id: newsProvider!.newsDetailModel!.id,
        content: content ?? '',
        success: (value) {
          kaiPrint('value--comment===${value}');
          Toast.show(context, message: 'Reply successful');
          if (value!.code == 0) {
            RemarkItem valueItem = RemarkItem.fromJson(value.data['comment']);
            // 需要修改父级的列表
            widget.onchange(valueItem);
            widget.remarkItem.comment_count += 1;
            if(widget.doCommentSuccess!=null){
              widget.doCommentSuccess!();
            }
            NativeChannelBridge().flGoCommentSuccess(widget.remarkItem.target_type.toString(),widget.articleId.toString(),'comment');
            setState(() {});
          }
        },
        fail: () {
          Toast.show(context,
              message: 'Network error, sending failure', status: '2');
        });
  }

  // 点赞，取消点赞
  doLike(RemarkItem remarkItem) {
    newsProvider!.doLike(
        target_id: remarkItem.id,
        target_type: 0,
        like_status: remarkItem.like_status,
        beforeCall: (value) {
          if (value) {
            remarkItem.like_count -= 1;
            remarkItem.like_status = 1;
          } else {
            remarkItem.like_count += 1;
            remarkItem.like_status = 0;
          }
          setState(() {});
        },
        failCall: (value) {
          if (value) {
            remarkItem.like_count += 1;
            remarkItem.like_status = 0;
          } else {
            remarkItem.like_count -= 1;
            remarkItem.like_status = 1;
          }
          setState(() {});
        });
  }
}
